package cn.com.chec.en.controller.admin;

import cn.com.chec.en.dao.TagInfoDao;
import cn.com.chec.en.model.domain.TagInfo;
import cn.com.chec.en.util.PageBean;
import cn.com.chec.en.util.StringTools;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({ "/admin/tag" })
public class TagInfoAdminController {
	protected static final Logger logger = Logger
			.getLogger(TagInfoAdminController.class);

	@Resource
	private TagInfoDao tagInfoDao;

	@RequestMapping({ "/list" })
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/channelInfo/tagList");

		String tag = StringUtils.isNotBlank(request.getParameter("tag")) ? request
				.getParameter("tag") : "";
		tag = StringTools.converISO2UTF8(tag);
		int curPage = StringUtils.isNotBlank(request.getParameter("curPage")) ? Integer
				.parseInt(request.getParameter("curPage")) : 1;
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		List<TagInfo> tagInfoList = this.tagInfoDao.getSubList(tag,
				pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(this.tagInfoDao.getCount(tag));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		map.put("tagInfoList", tagInfoList);
		map.put("pageBean", pageBean);
		map.put("tag", tag);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/showInsert" })
	public ModelAndView showInsert(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		map.put("act", "insert");
		return new ModelAndView("admin/channelInfo/tagOpr", map);
	}

	@RequestMapping({ "/insert" })
	public ModelAndView insert(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		String message = "";
		String viewName = "admin/common/message";
		String tag = StringUtils.isNotBlank(request.getParameter("tag")) ? request
				.getParameter("tag") : "";
		if (this.tagInfoDao.getCount(tag) > 0) {
			message = "\"" + tag + "\"  TAG关键词标签已经存在!&nbsp;&nbsp;<a href=\""
					+ request.getContextPath()
					+ "/admin/tag.do?act=showInsert\">返回</a>重新设置";
			map.put("message", message);
			return new ModelAndView(viewName, map);
		}
		TagInfo tagInfo = new TagInfo();
		tagInfo.setTag(tag);
		if (this.tagInfoDao.insert(tagInfo))
			message = "添加TAG关键字标签成功!";
		else {
			message = "添加TAG关键字标签失败!";
		}

		map.put("message", message);
		return new ModelAndView(viewName, map);
	}

	@RequestMapping({ "/showUpdate" })
	public ModelAndView showUpdate(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		int id = StringUtils.isNotBlank(request.getParameter("id")) ? Integer
				.valueOf(request.getParameter("id")).intValue() : 0;
		TagInfo tagInfo = this.tagInfoDao.getTagById(id);
		map.put("tagInfo", tagInfo);
		map.put("act", "update");
		return new ModelAndView("admin/channelInfo/tagOpr", map);
	}

	@RequestMapping({ "/update" })
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		int id = StringUtils.isNotBlank(request.getParameter("id")) ? Integer
				.valueOf(request.getParameter("id")).intValue() : 0;
		String tag = StringUtils.isNotBlank(request.getParameter("tag")) ? request
				.getParameter("tag") : "";
		String viewName = "admin/common/message";
		String message = "";
		if (this.tagInfoDao.getCount(tag) > 0) {
			message = "\"" + tag + "\"  TAG关键词标签已经存在!&nbsp;&nbsp;<a href=\""
					+ request.getContextPath()
					+ "/admin/tag.do?act=showUpdate&id=" + id + "\">返回</a>重新设置";
			map.put("message", message);
			return new ModelAndView(viewName, map);
		}
		TagInfo tagInfo = this.tagInfoDao.getTagById(id);
		tagInfo.setTag(tag);
		tagInfo.setId(id);
		if (this.tagInfoDao.update(tagInfo))
			message = "更新TAG关键字标签成功!";
		else {
			message = "更新TAG关键字标签失败!";
		}
		map.put("message", message);
		return new ModelAndView(viewName, map);
	}

	@RequestMapping({ "/del" })
	public ModelAndView del(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		int id = StringUtils.isNotBlank(request.getParameter("id")) ? Integer
				.valueOf(request.getParameter("id")).intValue() : 0;
		String viewName = "admin/common/message";
		String message = "";
		if (this.tagInfoDao.del(id))
			message = "删除TAG关键字标签成功!";
		else {
			message = "删除TAG关键字标签失败!";
		}
		map.put("message", message);
		return new ModelAndView(viewName, map);
	}
}